package com.calsoft.Hops_Mobile_Tablet;
/*
 * User: Minh Diep
 * Date: 6/26/13
 * Time: 1:28 PM
 */

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.calsoft.Hops_Mobile_Tablet.base.App;
import com.calsoft.Hops_Mobile_Tablet.component.AlertDialogFragment;
import com.calsoft.Hops_Mobile_Tablet.sql.LocationDbAdapter;
import com.calsoft.Hops_Mobile_Tablet.util.SharedPrefs;

public class LocationFragment extends Fragment {

    private TextView currentSetLocationTv;
    private SharedPrefs sPrefs;
    private LocationDbAdapter mDbHelper;
    private SimpleCursorAdapter dataAdapter;

    public LocationFragment() {
        // Empty constructor required for fragment subclasses
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_location, container, false);
        getActivity().setTitle("Location");

        currentSetLocationTv = (TextView) rootView.findViewById(R.id.location_current_set_tv);

        sPrefs = new SharedPrefs(getActivity().getApplicationContext());


        //Open our database
        mDbHelper = new LocationDbAdapter(getActivity());
        mDbHelper.open();
        displayLocationListView(rootView);
        return rootView;
    }

    private void displayLocationListView(View view) {

        ListView listView = (ListView) view.findViewById(R.id.location_listview);
        EditText inputFilter = (EditText) view.findViewById(R.id.location_filter);

        // TODO: channge this later to fetch only the cities based on the country name
        Cursor cursor = mDbHelper.fetchAllCities();

        dataAdapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_1, cursor,
                new String[]{LocationDbAdapter.CITY}, new int[]{android.R.id.text1}, 0);

        listView.setAdapter(dataAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> listView, View view,
                                    int position, long id) {
                // Get the cursor, positioned to the corresponding row in the result set
                Cursor cursor = (Cursor) listView.getItemAtPosition(position);

                // Get the CITY name from this row in the database.
                String city = cursor.getString(cursor.getColumnIndexOrThrow("City"));

                DialogFragment dialog = new AlertDialogFragment(App.DIALOG_SET_CURRENT_LOCATION, "Set location to " + city + "?", city);
                dialog.show(getActivity().getSupportFragmentManager(), "AlertDialogFragment");


            }
        });

        inputFilter.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
            }

            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start,
                                      int before, int count) {
                dataAdapter.getFilter().filter(s.toString());
            }
        });

        dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
            public Cursor runQuery(CharSequence constraint) {
                return mDbHelper.fetchCitiesByName(constraint.toString());
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        String currentLocationStr = sPrefs.getString(SharedPrefs.CURRENT_LOCATION, "Default Location");
        String currentCountryStr = sPrefs.getString(SharedPrefs.CURRENT_COUNTRY, "");
        String locationStr = (currentCountryStr.length() == 0)? currentLocationStr : currentLocationStr + ", " + currentCountryStr;
        currentSetLocationTv.setText(locationStr);
    }

    @Override
    public void onStop() {
        super.onStop();
        mDbHelper.close();
    }

    public void changeCurrentLocation(String strArg) {
        String currentCountryStr = sPrefs.getString(SharedPrefs.CURRENT_COUNTRY, "");
        String locationStr = (currentCountryStr.length() == 0)? strArg : strArg + ", " + currentCountryStr;
        currentSetLocationTv.setText(locationStr);
    }

    public void changeCountry() {
        Intent intent = new Intent(getActivity(), SubLocationActivity.class);
        startActivity(intent);
    }
}